package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.UserDTO;
import com.fuli.app.dto.query.UserQueryDTO;
import com.fuli.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags ="用户")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("增加用户")
    @PostMapping("/add")
    public ActionResult<String> add(@ApiParam("用户DTO") @RequestBody UserDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (dto.getAccount() == null && dto.getOpenId() == null) {
            errors.add(new ErrorInfo(0, "添加失败，用户账号和OpenId皆为空"));
        }
        if (dto.getAccount() != null && dto.getPassword() == null) {
            errors.add(new ErrorInfo(0, "密码为空"));
        }
        return userService.add(dto);

    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public ActionResult<String> delete(@ApiParam("用户id") @PathVariable String id) {
        return userService.delete(id);
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("/beachDelete")
    public ActionResult<List<String>> beachDelete(@ApiParam("用户id数组") @RequestParam List<String> ids) {
        if (StringUtils.isEmpty(ids))
            return new ActionResult<>(0, "用户id数组为空");
        return userService.beachDelete(ids);
    }

    @ApiOperation("修改用户")
    @PutMapping("/update")
    public ActionResult<String> update(@ApiParam("用户DTO") @RequestBody UserDTO dto) {
        List<ErrorInfo> errorInfos = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getId()))
            errorInfos.add(new ErrorInfo(0, "id为空"));
        if (errorInfos.size() > 0)
            return new ActionResult<>(errorInfos);
        return userService.update(dto);
    }

    @ApiOperation("获取用户")
    @GetMapping("/get/{id}")
    public ActionResult<UserDTO> get(@ApiParam("用户id") @RequestParam String id) {
        return userService.get(id);
    }

    @ApiOperation("分页获取")
    @GetMapping("/pageQuery")
    public ActionResult<Pagination<UserDTO>> pageQuery(@ApiParam("用户QueryDTO") @ModelAttribute UserQueryDTO dto) {
        if (dto.getPageNo() == null || dto.getPageNo() <= 0)
            dto.setPageNo(1);
        if (dto.getPageSize() == null || dto.getPageSize() <= 0)
            dto.setPageSize(10);
        return userService.pageQuery(dto);
    }

    @ApiOperation("获取指定账户是否已经存在")
    @GetMapping("/existAccount/{account}")
    public ActionResult<Boolean> existAccount(@ApiParam("账号") @RequestParam String account) {
        return userService.existAccount(account);
    }

    @ApiOperation("获取当前在线用户")
    @GetMapping("/current")
    public ActionResult<UserDTO> current() {
        UserDTO userDTO = (UserDTO) SecurityUtils.getSubject().getSession().getAttribute("user");
        return new ActionResult<>(userDTO);
    }

    @ApiOperation("重置密码")
    @PutMapping("/reset")
    public ActionResult<String> reset(@ApiParam("用户DTO") @RequestBody UserDTO dto){
        return userService.resetPassword(dto.getId(),dto.getPassword());
    }
}
